package com.wzh.pool;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @description:
 * @author: WZH
 * @time: 2021/5/24 17:48
 */
public class Demo2 {

    public static void main(String[] args) throws ExecutionException, InterruptedException {


        long s1 = System.currentTimeMillis();

//
//        int integer1 = 0;
//        Thread.sleep(100);
//        System.out.println("====");
//        for (int i = 0; i < 1000000; i++) {
//            integer1 += i;
//        }
//
//
//        int integer2 = 0;
//        Thread.sleep(100);
//        System.out.println("====");
//        for (int i = 0; i < 1000000; i++) {
//            integer2 += i;
//        }
//
//        int integer3 = 0;
//        Thread.sleep(100);
//        System.out.println("====");
//        for (int i = 0; i < 1000000; i++) {
//            integer3 += i;
//        }
//
//
//        int integer4 = 0;
//        Thread.sleep(100);
//        System.out.println("====");
//        for (int i = 0; i < 1000000; i++) {
//            integer4 += i;
//        }
//
//
//        int integer5 = 0;
//        Thread.sleep(100);
//        System.out.println("====");
//        for (int i = 0; i < 1000000; i++) {
//            integer5 += i;
//
//        }


        FutureTask<Integer> task1 = new FutureTask<>(new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                int k = 0;
                Thread.sleep(100);
                System.out.println("1");
                for (int i = 0; i < 10000000; i++) {
                    k += i;
                }
                return k;
            }
        });


        FutureTask<Integer> task2 = new FutureTask<>(new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                int k = 0;
                Thread.sleep(100);
                System.out.println("2");
                for (int i = 0; i < 10000000; i++) {
                    k += i;
                }
                return k;
            }
        });
        FutureTask<Integer> task3 = new FutureTask<>(new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                int k = 0;
                Thread.sleep(100);
                System.out.println("3");
                for (int i = 0; i < 10000000; i++) {
                    k += i;
                }
                return k;
            }
        });
        FutureTask<Integer> task4 = new FutureTask<>(new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                int k = 0;
                Thread.sleep(100);
                System.out.println("4");
                for (int i = 0; i < 10000000; i++) {
                    k += i;
                }
                return k;
            }
        });
        FutureTask<Integer> task5 = new FutureTask<>(new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                int k = 0;
                Thread.sleep(100);
                System.out.println("5");
                for (int i = 0; i < 10000000; i++) {
                    k += i;
                }
                return k;
            }
        });


        new Thread(task1).start();
        new Thread(task2).start();
        new Thread(task3).start();
        new Thread(task4).start();
        new Thread(task5).start();
        Integer integer1 = task1.get();
        Integer integer2 = task2.get();
        Integer integer3 = task3.get();
        Integer integer4 = task4.get();
        Integer integer5 = task5.get();
        System.out.println(integer1);
        System.out.println(integer2);
        System.out.println(integer3);
        System.out.println(integer4);
        System.out.println(integer5);
        long s2 = System.currentTimeMillis();
        System.out.println(s2-s1);

    }

    public void tt() throws ExecutionException, InterruptedException {
        long s1 = System.currentTimeMillis();
        FutureTask<Integer> task1 = new FutureTask<>(new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                int k = 0;
                Thread.sleep(100);
                System.out.println("1");
                for (int i = 0; i < 10000000; i++) {
                    k += i;
                }
                return k;
            }
        });


        FutureTask<Integer> task2 = new FutureTask<>(new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                int k = 0;
                Thread.sleep(100);
                System.out.println("2");
                for (int i = 0; i < 10000000; i++) {
                    k += i;
                }
                return k;
            }
        });
        FutureTask<Integer> task3 = new FutureTask<>(new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                int k = 0;
                Thread.sleep(100);
                System.out.println("3");
                for (int i = 0; i < 10000000; i++) {
                    k += i;
                }
                return k;
            }
        });
        FutureTask<Integer> task4 = new FutureTask<>(new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                int k = 0;
                Thread.sleep(100);
                System.out.println("4");
                for (int i = 0; i < 10000000; i++) {
                    k += i;
                }
                return k;
            }
        });
        FutureTask<Integer> task5 = new FutureTask<>(new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                int k = 0;
                Thread.sleep(100);
                System.out.println("5");
                for (int i = 0; i < 10000000; i++) {
                    k += i;
                }
                return k;
            }
        });


        new Thread(task1).start();
        new Thread(task2).start();
        new Thread(task3).start();
        new Thread(task4).start();
        new Thread(task5).start();
        Integer integer1 = task1.get();
        Integer integer2 = task2.get();
        Integer integer3 = task3.get();
        Integer integer4 = task4.get();
        Integer integer5 = task5.get();
        System.out.println(integer1);
        System.out.println(integer2);
        System.out.println(integer3);
        System.out.println(integer4);
        System.out.println(integer5);
        long s2 = System.currentTimeMillis();
        System.out.println(s2-s1);

    }
}
